iT邦幫忙

2022 iThome 鐵人賽

DAY 12
0
自我挑戰組

30天學習flutter系列 第 12

12.flutter的布局(五)

  • 分享至 

  • xImage
  •  

今天簡單的創建Todo頁面和User頁面

首先,我們創建ui資料夾,並創建檔案
https://ithelp.ithome.com.tw/upload/images/20220927/20108931Qb3yXRYIQ6.png

main.dart

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:todo/ui/screens/nav_screen.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: NavScreen(),
      debugShowCheckedModeBanner: false,
    );
  }
}

ui部分,將todo_item抽取出來

todo\lib\ui\components\todo_item_component.dart:

import 'dart:math';

import 'package:flutter/material.dart';

class TodoItem extends StatefulWidget {
  const TodoItem({Key? key}) : super(key: key);
  @override
  State<TodoItem> createState() => _TodoItemState();
}

class _TodoItemState extends State<TodoItem> {
  late Color color = Color(Random().nextInt(0xffffffff));

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const FlutterLogo(),
          Text('Todo item test'),
          TextButton(onPressed: () {}, child: Text('Done')),
          ElevatedButton(onPressed: () {}, child: Text('Delete')),
        ],
      ),
    );
  }
}

接著,我們來設計我們的UI

首先,設計我們的Nav Screen
todo\lib\ui\screens\nav_screen.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

import 'package:todo/ui/screens/home_screen.dart';
import 'package:todo/ui/screens/user_screen.dart';

class NavScreen extends StatefulWidget {
  const NavScreen({Key? key}) : super(key: key);

  @override
  State<NavScreen> createState() => _NavScreenState();
}

class _NavScreenState extends State<NavScreen> {
  final List<Widget> _screens = [
    HomeScreen(),
    UserScreen(),
  ];

  final List<IconData> _icons = [
    Icons.home,
    Icons.person,
  ];

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _icons.length,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Todo'),
        ),
        body: IndexedStack(
          index: _selectedIndex,
          children: _screens,
        ),
        bottomNavigationBar: TabBar(
          indicatorPadding: EdgeInsets.zero,
          indicator: BoxDecoration(
            border: Border(
              top: BorderSide(
                color: Colors.blue,
                width: 3.0,
              ),
            ),
          ),
          tabs: _icons
              .asMap()
              .map((i, e) => MapEntry(
                    i,
                    Tab(
                      icon: Icon(
                        e,
                        color:
                            i == _selectedIndex ? Colors.blue : Colors.black45,
                        size: 30.0,
                      ),
                    ),
                  ))
              .values
              .toList(),
          onTap: (index) => setState(() => _selectedIndex = index),
        ),
      ),
    );
  }
}


接著創建我們的home和user Screen

todo\lib\ui\screens\home_screen.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:todo/ui/components/todo_item_component.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final TrackingScrollController _trackingScrollController =
      TrackingScrollController();

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _trackingScrollController,
      slivers: [
        SliverAppBar(
          title: Text('Home Screen'),
        ),
        SliverToBoxAdapter(
          child: Column(
            children: [
              TodoItem(),
              TodoItem(),
              TodoItem(),
              TodoItem(),
              TodoItem(),
              TodoItem(),
            ],
          ),
        ),
      ],
    );
  }
}

todo\lib\ui\screens\user_screen.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class UserScreen extends StatefulWidget {
  const UserScreen({Key? key}) : super(key: key);

  @override
  State<UserScreen> createState() => _UserScreenState();
}

class _UserScreenState extends State<UserScreen> {
  final TrackingScrollController _trackingScrollController =
      TrackingScrollController();

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _trackingScrollController,
      slivers: [
        SliverAppBar(
          title: Text('User Screen'),
        ),
        SliverToBoxAdapter(
          child: Container(
            padding: const EdgeInsets.all(8.0),
            child: Row(children: [
              Placeholder(
                fallbackHeight: 50,
                fallbackWidth: 50,
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  children: [
                    Text('user: Hello'),
                  ],
                ),
              ),
            ]),
          ),
        ),
      ],
    );
  }
}

結果:
https://ithelp.ithome.com.tw/upload/images/20220927/20108931AuJt0DjBxF.png

https://ithelp.ithome.com.tw/upload/images/20220927/20108931ggWytFKrwY.png

今天簡單的製作出我們app的基本布局,明天接著說flutter元件內的狀態以及各種元件狀態互相傳遞的狀態管理


上一篇
11.flutter的布局(四)
下一篇
13.flutter的狀態管理(一)
系列文
30天學習flutter30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言